package com.awise.http;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * Created by Rover on 17/2/20.
 */

public class RoverGlobal {

    public SocketConnect connect;
    public boolean       connectFlag = false;

    public int           reConnectTimes = 0;    //设置重连次数

    public RoverInterfaceUser interfaceUser;

    public Context      globalContent  = null;

    /* 持有私有静态实例，防止被引用，此处赋值为null，目的是实现延迟加载 */
    private static RoverGlobal instance = null;

    /* 私有构造方法，防止被实例化 */
    private RoverGlobal() {
    }

    /* 1:懒汉式，静态工程方法，创建实例 */
    public static RoverGlobal getInstance() {
        if (instance == null) {
            instance = new RoverGlobal();
        }
        return instance;
    }

    public void initSocket(){
        connect = new SocketConnect(new SocketCallback() {
            @Override
            public void receive(byte[] buffer) {
//                System.out.println("收到的数据：" + buffer.length);
//                for(int i=0;i<buffer.length;i++){
//                    System.out.println("数据：" + buffer[i]);
//                }
                interfaceUser.Rover_receivedDataFromHost(buffer);
            }

            @Override
            public void disconnect() {
                Log.i("与设备断开连接","  fuck   fuck ");
                connectFlag = false;
                interfaceUser.Rover_connectHostFailed();
                if (listener!=null){
                    listener.disconnect();
                }
            }

            @Override
            public void connected() {
                Log.i("连接成功"," ");
                connectFlag = true;
                interfaceUser.Rover_connectHostSuccess();
                reConnectTimes = 0;
                if (listener!=null){
                    listener.connected();
                }
            }
        });
        connect.setRemoteAddress("192.168.3.26", 333);
        new Thread(connect).start();
    }


    //检测wifi是否打开
    public boolean isWifiOpened(Context context){
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager.isWifiEnabled()) {
            return true;
        } else {
            return false;
        }
    }

    //获取连接的wifi名称
    public String getConnectWifiSSID(Context context){
        WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (mWifiManager != null) {
            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
            //Log.i("连接的WiFi的名称 = "," = " + wifiInfo.getSSID());
            return wifiInfo.getSSID();
        }
        return null;
    }




    //获取累加和
    public byte getAddValue(byte[] bb,int length){
        byte temp = 0x00;
        for(int i=0;i<length;i++){
            temp+=bb[i];
        }
        return temp;
    }


    public interface onSocketListener{
        public void disconnect();
        public void connected();
    }

    private onSocketListener listener;

    public void setOnSocketListener(onSocketListener listener){
        this.listener=listener;

    }

}
